package pointtable

import "dev_gw/point"

type PointTable map[string]point.PointInterface

// type PointItem struct {
// 	route string
// 	point point.PointInterface
// }

func NewPointTable() PointTable {
	return make(PointTable, 0)
}

// func NewPointTable(...PointItem) PointTable {

// }

func (pt PointTable) isMatchRecursive(s string, p string, sIndex int, pIndex int) bool {
	if pIndex == len(p) {
		return sIndex == len(s)
	}

	if p[pIndex] == '*' {
		return pt.isMatchRecursive(s, p, sIndex, pIndex+1) || (sIndex < len(s)) && pt.isMatchRecursive(s, p, sIndex+1, pIndex)
	}

	if sIndex < len(s) && (p[pIndex] == s[sIndex] || p[pIndex] == '?') {
		return pt.isMatchRecursive(s, p, sIndex+1, pIndex+1)
	}

	return false
}

func (pt PointTable) isMatch(s string, p string) bool {
	return pt.isMatchRecursive(s, p, 0, 0)
}

func (pt PointTable) FindPoint(name string) point.PointInterface {
	for k, v := range pt {
		if pt.isMatch(name, k) {
			return v.SetRoute(name)
		}
	}

	return nil
}

func (pt PointTable) Insert(name string, p point.PointInterface) PointTable {
	pt[name] = p
	return pt
}
